/*
 * 原版接口
 */
import * as server_ui from "@minecraft/server-ui";
import * as server from "@minecraft/server";
/*
 * 蛋白石数据
 */
import * as data_format from "../data/format";
/*
 * 蛋白石组件
 */
import * as realm_property from "../system/realm_property";
import * as derived_tools from "../system/derived_tools";
import * as native_tools from "../system/native_tools";
import * as achieve_plan from "../system/achieve_plan";
import * as math_tools from "../system/math_tools";

/**
 * * 方块事件 向量弹射
 */
export function Vector(object: server.Block) {
    /**
     * * 实体查询选项
     */
    const setOptions: server.EntityQueryOptions = {
        location: object.center(),
        maxDistance: 1.5,
    };
    /**
     * * 获取实体队列
     */
    const entitys = object.dimension.getEntities(setOptions);
    // 对选中的实体进行向量弹射
    entitys.forEach(
        entity => {
            try {
                /**
                 * * 获取 向量
                 */
                const Vector = entity.getViewDirection();
                // 向量弹射
                entity.applyKnockback(Vector.x, Vector.z, (Math.abs(Vector.x) + Math.abs(Vector.z)) * 16, Math.abs(Vector.y) * 8);
                entity.addEffect('slow_falling', 60, { amplifier: 1, showParticles: false });
                entity.addEffect('resistance', 200, { amplifier: 31, showParticles: false });
                entity.addEffect('hunger', 200, { amplifier: 4, showParticles: false });
            }
            catch {
                // 向量弹射
                entity.applyImpulse({ x: 0, y: Math.random() + 0.1, z: 0 });
            }
        }
    )
};

/**
 * * 方块事件 区块显示
 */
export function Display(object: server.Block) {
    /**
     * * 检测当前区块是否有能量
     */
    const testEnergy = realm_property.AlterEnergy(object, 0, false);
    /**
     * * 设定 区块显示 的 粒子类型 
     */
    const showType = testEnergy[0] == true ? 'constant:pulse_rune_green' : 'constant:pulse_rune_red';
    // 显示 区块边界
    derived_tools.DisplayChunkBoundary(object);
    //显示烟雾效果
    native_tools.TrySpawnParticle(object.dimension, showType, math_tools.Vector.add(object, { x: 0.5, y: 0.5, z: 0.5 }));
};

/**
 * * 方块事件 火焰燃烧
 */
export function Flame(object: server.Block) {
    /**
     * * 获取 方块 周围 实体
     */
    const getEntities = object.dimension.getEntitiesAtBlockLocation(math_tools.Vector.add(object, math_tools.Vector.CONSTANT_UP));
    getEntities.forEach(entity => entity.setOnFire(20, true))
};

/**
 * * 方块事件 诸界道标
 */
export function RoadSignPresets(object: server.Block) {
    /**
     * * 诸界道标 数据信息
     */
    const RoadSign = new Map<string, data_format.LOCATION_AND_DIMENSION>();
    /**
     * * 获取 玩家
     */
    const player = object.dimension.getPlayers({ location: object.location, maxDistance: 8, closest: 1 })[0];
    // 检测玩家是否使用过诸界道标
    if (!achieve_plan.ControlEventTrigger.EntityToken('诸界道标:使用冷却', player, 20)) return;
    // 获取 所有 道标
    player.getDynamicPropertyIds().filter(id => id.startsWith('road_sign:')).forEach(id => native_tools.CompileSign(player, id, RoadSign));
    /**
     * * 获取 道标名称
     */
    const name = Array.from(RoadSign.keys()).map(id => `§n§o§l§${Math.floor(Math.random() * 6)}` + id.split(':')[1]);
    /** 
     * * 定义了 窗口界面 的 标题 
     */
    const title: server.RawMessage = {
        text: "§9<§l 诸界道标 §r§9>"
    };
    /** 
     * * 定义了 窗口界面 的 表单对象
     */
    const display = new server_ui.ActionFormData().title(title)
    name.forEach(info => display.button(info, "textures/物品贴图/魔法书籍/空间宝典"));
    if (name.length == 0) return player.onScreenDisplay.setTitle("§4暂无 §9道标信息§r")
    // 显示窗口界面
    display.show(player).then(
        option => {
            //检测玩家是否退出窗口
            if (option.canceled || option.selection == undefined) return
            /**
             * * 获取 道标参数
             */
            const value = Array.from(RoadSign.values());
            //执行传送流程 并 播放音效
            player.teleport(value[option.selection].location, { dimension: value[option.selection].dimension });
            server.system.runTimeout(() => player.playSound("mob.endermen.portal"), 5);
        }
    )
};